package com.study.talk.netty.message;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import com.study.talk.netty.handler.AcceptGroupResponseHandler;
import com.study.talk.netty.handler.AcceptJoinGroupResponseHandler;
import com.study.talk.netty.handler.AcceptResponseHandler;
import com.study.talk.netty.handler.AddUserResponseHandler;
import com.study.talk.netty.handler.AddUserSelfResponseHandler;
import com.study.talk.netty.handler.ApplyGroupResponseHandler;
import com.study.talk.netty.handler.ApplyGroupSelfResponseHandler;
import com.study.talk.netty.handler.CreateGroupResponseHandler;
import com.study.talk.netty.handler.GroupMessageResponseHandler;
import com.study.talk.netty.handler.HeartBeatResponseHandler;
import com.study.talk.netty.handler.InviteGroupResponseHandler;
import com.study.talk.netty.handler.InviteGroupSelfResponseHandler;
import com.study.talk.netty.handler.LogOutResponseHandler;
import com.study.talk.netty.handler.LoginResponseHandler;
import com.study.talk.netty.handler.MessageResponseHandler;
import com.study.talk.netty.handler.MessageSelfResponseHandler;
import com.study.talk.netty.handler.RegisterResponseHandler;
import com.study.talk.netty.handler.SmsSendResponseHandler;
import com.study.talk.netty.message.code.MessageDecoder;
import com.study.talk.netty.message.code.MessageEncoder;
import com.study.talk.netty.message.code.Splitter;
import com.study.talk.netty.message.request.HeartBeatRequest;
import com.study.talk.util.Constants;
import com.study.talk.util.EventBusUtil;
import com.study.talk.util.OtherMessage;


import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;

public class MyChannelInitializer extends ChannelInitializer<SocketChannel> {

    public MyChannelInitializer() {
    }

    @Override
    protected void initChannel(@NonNull SocketChannel ch) throws Exception {
        ch.pipeline().addLast(new Splitter())
                .addLast(new MessageDecoder())
                .addLast(new IdleStateHandler(0, 20, 0))
                .addLast(new ChannelDuplexHandler() {
                    @Override
                    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
                        IdleStateEvent event = (IdleStateEvent) evt;
                        if (event.state() == IdleState.WRITER_IDLE) {
                            ctx.channel().writeAndFlush(new HeartBeatRequest());
                        }
                    }
                })
                .addLast(new ChannelInboundHandlerAdapter() {
                    @Override
                    public void channelActive(@NonNull ChannelHandlerContext ctx) throws Exception {
                        EventBusUtil.sendEventDelayed(new OtherMessage(Constants.CONNECTION_SUCCESS), 1000);
                    }

                    @Override
                    public void channelRead(@NonNull ChannelHandlerContext ctx, @NonNull Object msg) throws Exception {
                        super.channelRead(ctx, msg);
                        Log.i("-------------", "channelRead: " + msg);
                    }
                })
                .addLast(new AcceptGroupResponseHandler())
                .addLast(new AcceptJoinGroupResponseHandler())
                .addLast(new AcceptResponseHandler())
                .addLast(new AddUserResponseHandler())
                .addLast(new AddUserSelfResponseHandler())
                .addLast(new ApplyGroupResponseHandler())
                .addLast(new ApplyGroupSelfResponseHandler())
                .addLast(new CreateGroupResponseHandler())
                .addLast(new GroupMessageResponseHandler())
                .addLast(new HeartBeatResponseHandler())
                .addLast(new InviteGroupResponseHandler())
                .addLast(new InviteGroupSelfResponseHandler())
                .addLast(new LogOutResponseHandler())
                .addLast(new LoginResponseHandler())
                .addLast(new MessageResponseHandler())
                .addLast(new MessageSelfResponseHandler())
                .addLast(new RegisterResponseHandler())
                .addLast(new SmsSendResponseHandler())
                .addLast(new MessageEncoder());
    }
}
